import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:my_salary/route/routes.dart';
import '../core/auth/user_manager.dart';
import '../route/route_utils.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class DrawerMenu extends StatelessWidget {
  const DrawerMenu({super.key});

  Widget _buildMenuItem(
      BuildContext context, IconData icon, String title, String route) {
    return ListTile(
      leading:
          Icon(icon, color: Theme.of(context).colorScheme.onSurfaceVariant),
      title: Text(
        title,
        style: Theme.of(context).textTheme.bodyMedium,
      ),
      onTap: () => RouteUtils.pushForNamed(context, route),
    );
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final color = Theme.of(context).colorScheme;
    var profile = UserManager().profile;

    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          DrawerHeader(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  color.primary,
                  color.primaryContainer,
                ],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
            ),
            margin: EdgeInsets.zero,
            padding: EdgeInsets.zero,
            child: SingleChildScrollView(
              padding: EdgeInsets.all(16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  GestureDetector(
                    onTap: () => {
                      if (profile != null)
                        RouteUtils.pushForNamed(context, RoutePath.profile)
                      else
                        RouteUtils.pushForNamed(context, RoutePath.login)
                    },
                    child: CircleAvatar(
                      radius: 30.r,
                      backgroundImage:
                          const AssetImage("assets/images/avatar.jpg"),
                    ),
                  ),
                  SizedBox(height: 10.h),
                  Text(
                    profile?.username ?? "请登录",
                    style: Theme.of(context)
                        .textTheme
                        .titleMedium
                        ?.copyWith(color: Colors.white),
                  ),
                  Text(
                    profile?.phone ?? "",
                    style: Theme.of(context)
                        .textTheme
                        .bodySmall
                        ?.copyWith(color: Colors.white70),
                  ),
                ],
              ),
            ),
          ),
          _buildMenuItem(context, Icons.home, l10n.menuHome, RoutePath.home),
          _buildMenuItem(context, Icons.calendar_month, "calendar", RoutePath.calendar),

          _buildMenuItem(
              context, Icons.timeline, l10n.menuTimeline, RoutePath.timeline),
          _buildMenuItem(context, Icons.query_stats, l10n.menuStatistics,
              RoutePath.statistics),
          _buildMenuItem(
              context, Icons.today_outlined, l10n.menuTodo, RoutePath.todo),
          _buildMenuItem(
              context, Icons.settings, l10n.menuSettings, RoutePath.settings),
          _buildMenuItem(context, Icons.info, l10n.menuAbout, RoutePath.about),
          const Divider(),
          _buildMenuItem(
              context, Icons.exit_to_app, l10n.menuLogout, RoutePath.logout),
        ],
      ),
    );
  }
}
